package com.zhima.typehandler;

import cn.hutool.json.JSONUtil;
import com.zhima.vo.SimpleLabelVO;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import static com.zhima.typehandler.ListGetNullableResult.listGetNullAbleResult;

/**
 * @author : 芝麻
 * @date : 2023-08-26 21:40
 **/
@MappedTypes(List.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class SimpleLabelVOTypeHandler extends BaseTypeHandler<List<SimpleLabelVO>> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSONUtil.toJsonStr(parameter));
    }


    @Override
    public List<SimpleLabelVO> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return listGetNullAbleResult(rs, columnName, SimpleLabelVO.class);
    }

    @Override
    public List<SimpleLabelVO> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return listGetNullAbleResult(rs, columnIndex, SimpleLabelVO.class);
    }

    @Override
    public List<SimpleLabelVO> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return listGetNullAbleResult(cs, columnIndex, SimpleLabelVO.class);
    }
}
